// pages/user/setting/setting.js
let app = getApp();
import Toast from '../../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
const areaJson = require('../../../utils/areaJson/area.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: '',
    nickname: '',
    mobile: '',
    sex: '',
    province: '',
    city: '',
    area: '',
    address: '',
    xiaoQu: '',
    sexOptions: [
      {text: '男', value: 1},
      {text: '女', value: 2}
    ],
    areaShow: false,//省市区弹框
    areaList: areaJson.area,//省市区数据，在area.josn获取
    isAll: 0,//是否显示 省市区、详细地址、小区
    lat:'',
    lng:'',
  },

  //保存
  saveSubmit(){
    let _this = this;

    if(!_this.data.nickname){
      Toast('请填写姓名'); return;
    } else if(!_this.data.mobile || !(/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(_this.data.mobile)) ){
      Toast('请填写正确电话'); return;
    }

    if(_this.data.isAll == 1){
      if(!(_this.data.province || _this.data.city || _this.data.area)){
        Toast('请选择省市区'); return;
      } else if(!_this.data.address){
        Toast('请填写详细地址'); return;
      }
    }

    app.requestUrl({
      method: 'POST',
      url: '/wxapi/member/update',
      data: {
        id: _this.data.user.id,
        realname: _this.data.nickname,
        gender: _this.data.sex,
        mobile: _this.data.mobile,
        province: _this.data.province,
        city: _this.data.city,
        area: _this.data.area,
        address: _this.data.address,
        xiaoqu: _this.data.xiaoQu,
        avatar: _this.data.user.avatar,
        lat:_this.data.user.lat,
        lng:_this.data.user.lng,
        houseNum:_this.data.houseNum
      },
      load: 1
    }).then(res =>{
      _this.data.user.nickname = _this.data.nickname;
      _this.data.user.gender = _this.data.sex;
      _this.data.user.mobile = _this.data.mobile;
      _this.data.user.province = _this.data.province;
      _this.data.user.city = _this.data.city;
      _this.data.user.area = _this.data.area;
      _this.data.user.address = _this.data.address;
      _this.data.user.xiaoqu = _this.data.xiaoQu;
      _this.data.user.houseNum = _this.data.houseNum;
      wx.setStorageSync('userInfo', JSON.stringify(_this.data.user));
      Toast('保存成功！');
    })
  },

  //省市区确定
  areaConfirm(event){
    let _this = this;
    _this.areaMonitor();
    _this.setData({
      province: event.detail.values[0].name,
      city: event.detail.values[1].name,
      area: event.detail.values[2].name,
    })
  },

  //省市区弹框
  areaMonitor(){
    let _this = this;
    _this.setData({
      areaShow: !_this.data.areaShow
    })
  },

  sexChange(event){
    let _this = this;
    let tempSex = event.detail;
    _this.setData({
      sex: tempSex
    })
  },

  nameBlur(event){
    this.setData({
      nickname: event.detail
    })
  },
  mobileBlur(event){
    this.setData({
      mobile: event.detail
    })
  },
  // addressBlur(event){
  //   this.setData({
  //     address: event.detail
  //   })
  // },
  xiaoQuBlur(event){
    this.setData({
      xiaoQu: event.detail
    })
  },
  houseNumBlur(event){
    this.setData({
      houseNum: event.detail
    })
  },

  //上传头像
  avatarAfterRead(event) {
    let _this = this;
    const { file } = event.detail;
    wx.uploadFile({
      url: app.globalData.deployUrl + '/platform/config/upload', // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'file',
      formData: { user: 'test' },
      success(res) {
        let data = JSON.parse(res.data)
        _this.setData({
          'user.avatar': data.key,
        })
      }
    });
  },
  // 详细地址
  call_map:function(){
    let _this = this;
    wx.getLocation({////获取当前位置信息
      type: 'wgs84',
      //获取位置成功
      success: function (res) {
        console.log(res)  //获取的的当前位置的详细信息
        wx.chooseLocation({////选择位置
          success: function (res) {
            let lat = 'user.lat';
            let lng = 'user.lng';
            console.log(res.latitude);
            _this.setData({
              address: res.name,
              [lat]: res.latitude,
              [lng]: res.longitude,
            });
          }
        })
      },
      //获取位置失败
      fail: function (err) {
        console.log("拒绝访问地址权限")
      },
      //接口调用结束的回调函数（调用成功、失败都会执行）
      complete: function (info) {
        console.log("完成")
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (event) {
    let _this = this;
    _this.setData({
      user: app.getUser(),
      nickname: app.getUser().nickname,
      sex: app.getUser().gender,
      mobile: app.getUser().mobile,
      province: app.getUser().province,
      city: app.getUser().city,
      area: app.getUser().area,
      address: app.getUser().address,
      xiaoQu: app.getUser().xiaoqu != 'undefined' ? app.getUser().xiaoqu : '',
      isAll: event.isAll ? event.isAll : 0
    })
    console.log(app.getUser());
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})